package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/productSpec")
@Tag(name = "商品规格接口管理")
public class ProductSpecController extends BaseController {
    @Resource
    private ProductSpecService productSpecService;

   /* @Operation(summary = "条件查询规格分页列表")
    @GetMapping("list")
    public TableDataInfo list(ProductSpec productSpec) {
        List<ProductSpec> list = productSpecService.listProductSpec(productSpec);
        return getDataTable(list);
    }*/

    @Operation(summary = "查询商品规格列表")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpec productSpec) {
        startPage();
        List<ProductSpec> list = productSpecService.selectProductSpecList(productSpec);
        return getDataTable(list);
    }


    @Operation(summary = "获取商品规格详细信息")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(productSpecService.selectProductSpecById(id));
    }

    @Operation(summary = "新增商品规格")
    @PostMapping
    public AjaxResult add(@RequestBody @Validated ProductSpec productSpec) {
        productSpec.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.save(productSpec));
    }

    @Operation(summary = "修改商品规格")
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated ProductSpec productSpec) {
        productSpec.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.updateById(productSpec));
    }

    @Operation(summary = "删除商品规格")
    @DeleteMapping("/ids")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(productSpecService.removeBatchByIds(Arrays.asList(ids)));

    }
}

/*
@RestController
@RequestMapping("spec")
@Tag(name = "商品规格管理模块")
public class ProductSpecController extends BaseController {
    @Resource
    ProductSpecService productSpecService;
    @Operation(summary = "新增商品：根据三级分类id查询分类下的规格参数列表")
    @GetMapping("listSpecsByC3id/{c3id}")
    public AjaxResult listSpecsByC3id(@PathVariable("c3id")Long c3id) {
        //单表操作，直接使用mp自带的方法完成
        //多表操作：mp不便于实现，一般自定义sql。  需要我们自定义业务 mapper方法实现
        return success(
                productSpecService
                        .list(Wrappers
                                .lambdaQuery(ProductSpec.class)
                                .eq(ProductSpec::getCategoryId , c3id)
                        )
        );
    }
    @Operation(summary = "条件查询规格分页列表")
    @GetMapping("list")
    public TableDataInfo list(ProductSpec productSpec) {
        List<ProductSpec> list = (List<ProductSpec>) productSpecService.listProductSpec(productSpec);
        return getDataTable(list);
    }


    @Operation(summary = "根据id查询规格")
    @GetMapping("{id}")
    public AjaxResult getById(@PathVariable Long id) {
        ProductSpec spec = productSpecService.selectSpecById(id);
        return success(spec);
    }

    @Operation(summary = "批量删除规格")
    @DeleteMapping("{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productSpecService.removeBatchByIds(ids));
    }

    @Operation(summary = "新增规格")
    @PostMapping
    public AjaxResult save(@Validated @RequestBody ProductSpec productSpec) {
        //同一三级分类下规格名称不能重复
        if (!productSpecService.checkSpecIsUnique(productSpec)) {
            return error("新增规格失败，分类下已存在规格参数：" + productSpec.getSpecName());
        }
        productSpec.setCreateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.save(productSpec));
    }

    //    @Operation(description = "更新规格")
    @Operation(summary = "更新规格")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ProductSpec productSpec) {
        //同一三级分类下规格名称不能重复
        if (!productSpecService.checkSpecIsUnique(productSpec)) {
            return error("更新规格失败，分类下已存在规格参数：" + productSpec.getSpecName());
        }
        productSpec.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(productSpecService.updateById(productSpec));
    }
}
*/
